package com.geeksaga.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.geeksaga.common.util.CacheUtil;
import com.geeksaga.dao.BbsEnvDao;
import com.geeksaga.domain.BbsEnv;

/**
 * @author newbie
 * @since 0.1
 */
@Service
public class BbsEnvServiceImpl implements BbsEnvService {

    @Autowired private BbsEnvDao bbsEnvDao;

    /* (non-Javadoc)
     * @see com.geeksaga.service.BbsEnvService#loadCache()
     */
    public void loadCache() throws DataAccessException {
        BbsEnv env = bbsEnvDao.find();

        if(env != null)
            CacheUtil.put(CacheUtil.CACHE, CacheUtil.KEY_BBS_ENV, env);
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.service.BbsEnvService#find()
     */
    public BbsEnv find() {
        BbsEnv cacheEnv = CacheUtil.get(CacheUtil.CACHE, CacheUtil.KEY_BBS_ENV);
        
        if(cacheEnv == null) {
            loadCache();
            
            cacheEnv = CacheUtil.get(CacheUtil.CACHE, CacheUtil.KEY_BBS_ENV);
        }
        
        return cacheEnv;
    }
}